﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace GameDevelopHelper.Common
{
    class LoginSection : ConfigurationSection
    { 
    
    }

    public class AppConfiguration
    {
        private static AppConfiguration m_Instance;
        private Configuration m_Config;

        static AppConfiguration()
        { 
        
        }

        public static AppConfiguration Instance()
        {
            if (m_Instance == null)
            {
                m_Instance = new AppConfiguration();
            }

            return m_Instance;
        }

        public bool InitConfiguration(string strPath = "")
        {
            if (strPath == "")
            {
                m_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            else
            {
                m_Config = ConfigurationManager.OpenExeConfiguration(strPath);
            }
            
            if (m_Config == null)
            {
                return false;
            }

            return true;
        }

        // 判断appSettings中是否有此项
        private bool AppSettingsKeyExists(string strKey)
        {
            foreach (string str in m_Config.AppSettings.Settings.AllKeys)
            {
                if (str == strKey)
                {
                    return true;
                }
            }
            return false;
        }

        // 判断Section是否存在，不存在则创建
        private bool AppSectionExists(string strSection, bool bIsNotExistCreate)
        {
            for (int i = 0; i < m_Config.Sections.Count; i++)
            {
                if (strSection == m_Config.Sections[strSection].SectionInformation.SectionName)
                {
                    return true;
                }
            }

            if (bIsNotExistCreate == true)
            {
                m_Config.AppSettings.Settings.Add("strSection", "xxx");
            }
        }


        /*******************************************************************
         * - 登录窗口
         *******************************************************************/
        private const string strLoginSectionName = "Login";
        private const string strAutoLoginKeyName = "AutoLogin";

        //自动登录
        public void WriteAutoLogin(bool bCheck)
        { 
            
        }

        public bool ReadAutoLogin()
        {
            if (AppSettingsKeyExists(strAutoLoginKeyName) == true)
            {
                //return ConfigurationManager.AppSettings[strAutoLoginKeyName];
            }

            return true;
        }


    }
}
